<template>
	<tm-fullView bg-color="#ffffff">
		<tm-menubars :title="$t('注册用户')" iconColor="white" color="#ffffff" :showback="false">
			
		</tm-menubars>
		<view class="logo">
			<image src="/static/logo.png"></image>
		</view>
		<view class="login-form">
			<tm-input name="title" 
			prefixp-icon="myicon-youjianyouxiang" 
			prefixp-icon-color="grey-darken-1" 
			prefixp-icon-size="40"
			:placeholder="$t('请输入邮箱')" 
			:height="80" :round="20" 
			:border-bottom="false" 
			bg-color="grey-lighten-3" 
			v-model="reqData.email"/>
			<view style="height:40rpx;"></view>
			<tm-input
			bg-color="grey-lighten-3" 
			prefixp-icon="myicon-yanzhengma"
			prefixp-icon-color="grey-darken-1" 
			prefixp-icon-size="40"
			:border-bottom="false" 
			input-type="number" 
			:height="80"
			:round="20"
			:vertical="true" 
			v-model="reqData.code"
			:placeholder="$t('请输入验证码')">
				<template v-slot:rightBtn>
					<tm-button class="ml-20" theme="grey-darken-2" @click="sendCode" :round="8" :font-size="16"  :height="80" block :width="180">
						<tm-countdown ref="daojishi" :autoStart="false" :time="120*1000">
							<template v-slot:default="{timeData}">
								{{timeData.finish==false&&timeData.data.seconds!=='00'?'剩'+timeData.data.seconds+'秒':$t('获取验证码')}}
							</template>
						</tm-countdown>
					</tm-button>
				</template>
			</tm-input>
			<tm-input name="title" 
			prefixp-icon="myicon-mima" 
			prefixp-icon-color="grey-darken-1" 
			prefixp-icon-size="40" 
			:placeholder="$t('请输入密码')" 
			:height="80" 
			:round="20" 
			:border-bottom="false" 
			bg-color="grey-lighten-3" 
			  v-model="reqData.password"/>
			<view class="login-submit">
				<tm-button theme="grey-darken-2" :round="24" @click="register" :loading="loading" block>{{$t("注册用户")}}</tm-button>
				<view class="agreement">
					<tm-checkbox v-model="reqData.checked" :dense="true" :size="24"></tm-checkbox>
					<text class="text">{{$t('请阅读并同意')}}</text>
					<text class="link">{{$t('用户协议')}}</text>
					<text class="text">{{$t('和')}}</text>
					<text class="link">{{$t('隐私协议')}}</text>
				</view>
			</view>
		</view>
		
		<view class="flex-center mt-50">
			<text @click="back()" class="text-size-s text-grey-darken-1">{{$t('返回登录')}}</text>
		</view>
	</tm-fullView>
</template>

<script>
	import IndexApi from '@/api/index.js'
	export default {
		data() {
			return {
				loading:false,
				reqData:{
					email:'2518687673@qq.com',
					password:'',
					checked:false,
					code:''
				}
			}
		},
		methods: {
			sendCode(){
				let self=this
				if(this.reqData.email==''){
					uni.showToast({
						title:self.$t('请输入你的邮箱'),
						icon:'none'
					})
					return
				}
				uni.showLoading({
					title:self.$t('正在获取验证码')
				})
				IndexApi.sendMailCode({email:self.reqData.email}).then(res=>{
					uni.hideLoading()
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					if(res.code==200){
						self.$refs.daojishi.start()
					}
				})
			},
			register(){
				let self=this
				if(this.reqData.email==''){
					uni.showToast({
						title:self.$t('请输入你的邮箱'),
						icon:'none'
					})
					return
				}
				if(this.reqData.code==''){
					uni.showToast({
						title:self.$t('请输入验证码'),
						icon:'none'
					})
					return
				}
				if(this.reqData.password==''){
					uni.showToast({
						title:self.$t('请输入密码'),
						icon:'none'
					})
					return
				}
				if(!this.reqData.checked){
					uni.showToast({
						title:self.$t('请同意用户协议和隐私协议'),
						icon:'none'
					})
					return
				}
				self.loading=true
				IndexApi.register(self.reqData).then(res=>{
					self.loading=false
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					if(res.code==200){
						setTimeout(function(){
							self.back()
						},1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.logo{
	display: flex;
	flex-direction: row;
	justify-content: center;
	align-items: center;
	padding: 80rpx 20rpx;
	image{
		width: 160rpx;
		height: 160rpx;
	}
}
.login-form{
	padding: 20rpx 40rpx;
	.login-submit{
		margin-top: 80rpx;
		padding: 0 40rpx;
		.agreement{
			margin-top: 30rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			padding-left: 20rpx;
			.text{
				font-size: 24rpx;
			}
			.link{
				font-size: 24rpx;
				color: #2196F3;
				text-decoration: underline
			}
		}
	}
}
.third{
	text-align: center;
	margin-top: 130rpx;
}
</style>
